<template>


  <el-form ref="scriptForm" :model="scriptForm" :rules="scriptRules" label-width="80px">

    <el-form-item label="名称" prop="sceneName">
      <el-input v-model="sceneName"/>
    </el-form-item>

    <el-form-item label="说明" prop="illustrate">
      <el-input v-model="illustrate"/>
    </el-form-item>

    <el-form-item v-if="sceneType === 1" label="用户输入参数示例" prop="userInputParam">
      <vue-json-editor v-model="userInputParam" />
    </el-form-item>


 <!--分割线---------------电路筛选、电路生成----------------------------------------------------------------    --------------->

    <el-collapse style="border-top: 0px" v-model="activeNames" >
      <el-collapse-item v-if="sceneType === 2 || sceneType === 3" title="查看输入参数列表" name="1">
        <el-table
          :max-height="300"
          :data="inputParamList.filter(data => !search || (data.name.toLowerCase().includes(search.toLowerCase()) || data.code.toLowerCase().includes(search.toLowerCase())))"
          style="width: 100%">
          <el-table-column
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="编码"
            prop="code">
            <template  slot-scope="scope">
              <span>{{"all." + GlobalVariable.getOneCircuitTypeCode(scope.row.circuitType.circuitType) + ".input." + scope.row.code}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="电路类型"
            prop="dataType">
            <template  slot-scope="scope">
              <span>{{GlobalVariable.getOneCircuitType(scope.row.circuitType.circuitType)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="数据类型"
            prop="dataType">
            <template  slot-scope="scope">
              <span>{{GlobalVariable.getOneDataType(scope.row.dataType)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="right">
            <template slot="header" slot-scope="scope">
              <el-input
                v-model="search"
                size="mini"
                placeholder="输入关键字搜索（名称或者编码）"/>
            </template>
          </el-table-column>
        </el-table>

      </el-collapse-item>
      <el-collapse-item v-if="sceneType === 2 || sceneType === 3" title="查看属性列表" name="2">
        <el-table
          :max-height="300"
          :data="attributeList.filter(data => !search || (data.name.toLowerCase().includes(search.toLowerCase()) || data.code.toLowerCase().includes(search.toLowerCase())))"
          style="width: 100%">
          <el-table-column
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="编码"
            prop="code">
            <template  slot-scope="scope">
              <span>{{"all." + GlobalVariable.getOneCircuitTypeCode(scope.row.circuitType.circuitType) + ".prop." + scope.row.code}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="电路类型"
            prop="dataType">
            <template  slot-scope="scope">
              <span>{{GlobalVariable.getOneCircuitType(scope.row.circuitType.circuitType)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="数据类型"
            prop="dataType">
            <template  slot-scope="scope">
              <span>{{GlobalVariable.getOneDataType(scope.row.dataType)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="right">
            <template slot="header" slot-scope="scope">
              <el-input
                v-model="search"
                size="mini"
                placeholder="输入关键字搜索（名称或者编码）"/>
            </template>
          </el-table-column>
        </el-table>

      </el-collapse-item>
      <el-collapse-item v-if="sceneType === 3" title="查看输出参数列表" name="3">
        <el-table
          :max-height="300"
          :data="paramList.filter(data => !search || (data.name.toLowerCase().includes(search.toLowerCase()) || data.code.toLowerCase().includes(search.toLowerCase())))"
          style="width: 100%">
          <el-table-column
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            label="编码"
            prop="code">
          </el-table-column>
          <el-table-column
            label="数据类型"
            prop="dataType">
            <template  slot-scope="scope">
              <span>{{GlobalVariable.getOneDataType(scope.row.dataType)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="right">
            <template slot="header" slot-scope="scope">
              <el-input
                v-model="search"
                size="mini"
                placeholder="输入关键字搜索（名称或者编码）"/>
            </template>
          </el-table-column>
        </el-table>

      </el-collapse-item>
    </el-collapse>

<!--    <el-form-item v-if="sceneType === 2 || sceneType === 3" label="输入参数列表" prop="inputParam">-->
<!--      <el-select style="width:500px" v-model="inputParam" placeholder="" size="medium" filterable>-->
<!--        <el-option v-for="(item, index) in inputParamList"-->
<!--                   :label="item.name + ' (' + item.code + ') - ' + GlobalVariable.getOneDataType(item.dataType)"-->
<!--                   :value="item.id">-->
<!--          <span style="float: left">{{item.name + ' (' + item.code + ')'}}</span>-->
<!--          <span-->
<!--            style="float: right; color: #8492a6; font-size: 13px">{{ GlobalVariable.getOneDataType(item.dataType) }}</span>-->
<!--        </el-option>-->
<!--      </el-select>-->
<!--    </el-form-item>-->

<!--    <el-form-item v-if="sceneType === 2 || sceneType === 3" label="属性列表" prop="attribute">-->
<!--      <el-select style="width:500px" v-model="attribute" placeholder="" size="medium" filterable>-->
<!--        <el-option v-for="(item, index) in attributeList"-->
<!--                   :value="item.id"-->
<!--                   :label="item.name + ' (' + item.code + ') - ' + GlobalVariable.getOneDataType(item.dataType)">-->
<!--          <span style="float: left">{{item.name + ' (' + item.code + ')'}}</span>-->
<!--          <span-->
<!--            style="float: right; color: #8492a6; font-size: 13px">{{ GlobalVariable.getOneDataType(item.dataType) }}</span>-->
<!--        </el-option>-->
<!--      </el-select>-->
<!--    </el-form-item>-->

<!--分割线---------------电路筛选、电路生成----------------------------------------------------------------    --------------->



<!--分割线---------------电路生成----------------------------------------------------------------    --------------->

<!--    <el-form-item v-if="sceneType === 3" label="输出参数列表" prop="attribute">-->
<!--      <el-select style="width:500px" v-model="param" placeholder="" size="medium" filterable>-->
<!--        <el-option v-for="(item, index) in paramList"-->
<!--                   :value="item.id"-->
<!--                   :label="item.name + ' (' + item.code + ') - ' + getOneComponentType(item.componentType) + ' - ' + GlobalVariable.getOneDataType(item.dataType)">-->
<!--          <span style="float: left">{{item.name + ' (' + item.code + ')' + ' - ' + getOneComponentType(item.componentType)}}</span>-->
<!--          <span-->
<!--            style="float: right; color: #8492a6; font-size: 13px">{{ GlobalVariable.getOneDataType(item.dataType) }}</span>-->
<!--        </el-option>-->
<!--      </el-select>-->
<!--    </el-form-item>-->

<!--分割线---------------电路生成------------------------------------------------------------------------------->


    <!--      <el-form-item label="输入参数" prop="inputParameter">-->
    <!--        <el-input v-model="scriptForm.inputParameter" disabled/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="输入变量" prop="variable" v-if="scriptForm.inputParameter">-->
    <!--        <el-input v-model="scriptForm.variable" placeholder="请输入参数,格式：参数1、参数2"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="输出参数" prop="outputParameter">-->
    <!--        <el-input v-model="scriptForm.outputParameter" disabled/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item label="说明" prop="illustrate">-->
    <!--        <el-input v-model="scriptForm.illustrate" :rows="3" type="textarea" disabled/>-->
    <!--      </el-form-item>-->


    <div v-for="(item, index) in scriptList">

      <!--其他脚本类型-->
      <div :class="{'jsbox':item.state === 'YES', 'jsbox_off': item.state === 'NO'}"
           v-if="item.scriptType !== 'CONDITION'">
        <el-row class="jsbox_titles">
          <el-col :span="3">
            <el-form-item label="脚本" :prop="'priority' + index">
              <el-input v-model="item.priority" type="number" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item class="title_form" label="" :prop="'scriptType' + index">
              <i
                :class="{'el-icon-s-flag myflog1':item.scriptType === 'ASSIGNMENT', 'el-icon-s-flag myflog2': item.scriptType === 'RANGE', 'el-icon-s-flag myflog3': item.scriptType === 'CONDITION', 'el-icon-s-flag myflog4': item.scriptType === 'FORMULA', 'el-icon-s-flag myflog5': item.scriptType === 'CUSTOM'}"></i>
              <span class="myname_color">脚本类型：</span>
              <el-select v-model="item.scriptType" placeholder="请选择类型" size="small">
                <el-option v-for="(typeItem, typeIndex) in scriptTypeList" :label="typeItem.name"
                           :value="typeItem.value"/>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="11" style="text-align: right;">

            <el-switch
              v-model="item.state"
              active-value="YES"
              inactive-value="NO"
              :inactive-text="item.state === 'YES' ? '已启用' : (item.state === 'NO' ? '未启用' : '')"
            >
            </el-switch>
            <el-button size="mini" v-if="index > 0" @click="scriptUpward(index)" type="primary"
                       style="margin-bottom: 20px;margin-top: 5px!important;" icon="el-icon-top">上移
            </el-button>
            <el-button size="mini" v-if="index !== scriptList.length - 1" @click="scriptDownward(index)" type="primary"
                       style="margin-bottom: 20px;margin-top: 10px;" icon="el-icon-bottom">下移
            </el-button>
            <el-button size="mini" @click="deleteScript(index)" type="danger"
                       style="margin-bottom: 20px;margin-top: 5px!important;" icon="el-icon-delete-solid">删除
            </el-button>
          </el-col>
        </el-row>

        <div class="boxborder1">
          <!--脚本内容-->
          <div style=" padding:0 15px 0px 15px;">
            <el-form-item label="运行脚本" :prop="'runScript' + index">
              <el-input v-model="item.runScript" :rows="2" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </div>

        </div>
      </div>

      <!--条件脚本类型-->
      <div :class="{'jsbox':item.state === 'YES', 'jsbox_off': item.state === 'NO'}" v-else>

        <el-row class="jsbox_titles">
          <el-col :span="3">
            <el-form-item label="脚本" :prop="'priority' + index">
              <el-input v-model="item.priority" type="number" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <div id="mycolor">
              <el-form-item class="title_form" label="" :prop="'scriptType' + index">
                <i
                  :class="{'el-icon-s-flag myflog1':item.scriptType === 'ASSIGNMENT', 'el-icon-s-flag myflog2': item.scriptType === 'RANGE', 'el-icon-s-flag myflog3': item.scriptType === 'CONDITION', 'el-icon-s-flag myflog4': item.scriptType === 'FORMULA', 'el-icon-s-flag myflog5': item.scriptType === 'CUSTOM'}"></i>
                <span class="myname_color">脚本类型：</span>
                <el-select v-model="item.scriptType" placeholder="请选择类型" size="small">
                  <el-option v-for="(typeItem, typeIndex) in scriptTypeList" :label="typeItem.name"
                             :value="typeItem.value"/>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="11" style="text-align: right;">
            <el-switch
              v-model="item.state"
              active-value="YES"
              inactive-value="NO"
              :inactive-text="item.state === 'YES' ? '已启用' : (item.state === 'NO' ? '未启用' : '')"
            >
            </el-switch>

            <el-button size="mini" v-if="index > 0" @click="scriptUpward(index)" type="primary"
                       style="margin-bottom: 20px;margin-top: 5px!important;" icon="el-icon-top">上移
            </el-button>
            <el-button size="mini" v-if="index !== scriptList.length - 1" @click="scriptDownward(index)" type="primary"
                       style="margin-bottom: 20px;margin-top: 10px;" icon="el-icon-bottom">下移
            </el-button>
            <el-button size="mini" @click="deleteScript(index)" type="danger"
                       style="margin-bottom: 20px;margin-top: 5px!important;" icon="el-icon-delete-solid">删除
            </el-button>
          </el-col>
        </el-row>

        <div class="boxborder1">
          <div style=" padding:0 15px 15px 15px;">
            <!--类型信息-->


            <div v-for="(conditionItem, conditionIndex) in item.conditionList">
              <div class="condition_title">
                <el-row class="">
                  <el-col :span="3">
                    <span class="condition_title_text">条件：{{conditionIndex+1}}</span>
                  </el-col>
                  <el-col :span="9">
                    <!--条件类型信息-->
                    <el-form-item label="条件类型" :prop="'conditionType' + index + '' + conditionIndex">
                      <el-select v-model="conditionItem.conditionType" placeholder="请选择类型" clearable size="small">
                        <el-option v-for="(conditionTypeItem, conditionTypeIndex) in conditionTypeList"
                                   :label="conditionTypeItem" :value="conditionTypeIndex"/>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12" style="text-align: right;">
                    <el-button size="mini" icon="el-icon-top" v-if="conditionIndex > 0"
                               @click="conditionUpward(conditionIndex, item.conditionList)" type="warning"
                               style="margin-bottom: 20px;margin-top: 10px;">上移
                    </el-button>
                    <el-button size="mini" icon="el-icon-bottom" v-if="conditionIndex !== item.conditionList.length - 1"
                               @click="conditionDownward(conditionIndex, item.conditionList)" type="warning"
                               style="margin-bottom: 20px;margin-top: 10px;">下移
                    </el-button>
                    <el-button size="mini" icon="el-icon-close" v-if="item.conditionList.length > 1"
                               @click="deleteCondition(item.conditionList, conditionIndex)" type="danger"
                               style="margin-bottom: 20px;">删除
                    </el-button>
                  </el-col>
                </el-row>

              </div>


              <el-row :gutter="10">
                <el-col style="width: 49%;" v-if="conditionItem.conditionType !== 2">
                  <div>条件脚本</div>
                  <el-form-item class="mybox_s1" label="" :prop="'runScript' + index + '' + conditionIndex">
                    <el-input v-model="conditionItem.conditionScript" :rows="3" type="textarea" placeholder="请输入内容"/>
                  </el-form-item>
                </el-col>

                <el-col style="width: 2%;" v-if="conditionItem.conditionType !== 2">
                  <i class="el-icon-d-arrow-right" style="margin: 60px auto 0 0px;"></i>
                </el-col>


                <el-col style="width: 49%;" v-if="conditionItem.conditionType !== 2">
                  <div>执行脚本</div>
                  <el-form-item class="mybox_s2" label="" :prop="'executeScript' + index + '' + conditionIndex">
                    <el-input v-model="conditionItem.execute" :rows="3" type="textarea" placeholder="请输入内容"/>
                  </el-form-item>
                </el-col>


                <el-col style="width: 100%;" v-if="conditionItem.conditionType == 2">
                  <div>执行脚本</div>
                  <el-form-item class="mybox_s2" label="" :prop="'executeScript' + index + '' + conditionIndex">
                    <el-input v-model="conditionItem.execute" :rows="3" type="textarea" placeholder="请输入内容"/>
                  </el-form-item>
                </el-col>

              </el-row>

              <!-- <el-form-item v-if="conditionItem.conditionType !== 2" label="条件脚本" :prop="'runScript' + index + '' + conditionIndex">
                <el-input v-model="conditionItem.conditionScript" :rows="1" type="textarea" placeholder="请输入内容"/>
              </el-form-item>

              <el-form-item  label="执行脚本" :prop="'executeScript' + index + '' + conditionIndex">
                <el-input v-model="conditionItem.execute" :rows="3" type="textarea" placeholder="请输入内容"/>
              </el-form-item> -->


            </div>
            <div style="text-align: right; margin-right: 20px;">
              <el-button style="margin-bottom: 0px" type="primary" @click="addCondition(item.conditionList)">添加条件
              </el-button>
            </div>


          </div>
        </div>

      </div>


    </div>

    <div style="text-align: right;margin-top: 20px;">
      <el-button type="primary" @click="addScript" style="margin-bottom: 20px">添加脚本</el-button>
    </div>


    <!--      <el-form-item label="输出结果" prop="result">-->
    <!--        <el-input v-model="scriptForm.result" :rows="5" type="textarea"/>-->
    <!--      </el-form-item>-->

    <div>
      <el-button type="primary" @click="save">保 存</el-button>
      <el-button @click="close">关 闭</el-button>
    </div>
  </el-form>


</template>

<script>
  import {allListParam} from "@/api/expert/param";
  import {allListTemplate} from "@/api/expert/codeDataTemplate";
  import {listScript, addAllScript, getScript, delScript, addScript, updateScript} from "@/api/expert/script";
  import { getDicts } from '@/api/system/dict/data'
  import vueJsonEditor from 'vue-json-editor'

  export default {
    name: "ruleScriptCommon",
    components:{vueJsonEditor},
    props: {
      sceneType: {
        type: Number,
        default: 1
      },
      sceneId: {
        type: Number,
        default: null
      },
      sceneName: {
        type: String,
        default: null
      },
      circuitTypeId: {
        type: Number,
        default: null
      },
    },
    data() {
      return {
        scriptForm: {},
        scriptRules: {},

        // 脚本数据列表
        scriptList: [],
        // 脚本类型列表
        scriptTypeList: undefined,
        // 条件类型
        conditionTypeList: ["如果", "否则如果", "否则"],
        // 场景类型名称
        sceneTypeString: "",

        // 属性列表
        attributeList: [],
        // 输入参数列表
        inputParamList: [],
        // 属性
        attribute: null,
        // 输入参数
        inputParam: null,

        // 元器件生成输出参数
        paramList: [],
        // 输出参数
        param: null,
        // 元器件类型
        componentTypeList: [],

        // 说明
        illustrate: "",

        // 折叠
        activeNames: [],
        // 搜索框
        search: "",
        // 用户输入参数
        userInputParam: {
          "all.user.input.bus":"总线通信类型",
          "all.user.input.volt":"电压等级",
          "all.user.input.analoguenum":"采样通道数（模拟）",
          "all.user.input.digitalnum":"采样通道数（数字）",
          "all.user.input.controller":"处理器的类型",
          "all.user.input.prior":"设计考虑",
          "all.user.input.qualitygrade":"质量等级",
          "all.user.input.power":"最大功率",
          "all.user.input.analogue(模拟通道)":[{
            "analogueno":"通道数",
            "digitalname":"采集信号名称",
            "analoguetype":"采集信号类型",
            "sample":"采样率",
            "signalmax":"信号带宽最大频率",
            "signalmin":"信号带宽最小频率",
            "maxdatarange":"数据范围最大值",
            "mindatarange":"数据范围最小值",
            "samplingbit":"采样分辨率",
            "samplinglength":"采样长度"
          }],
          "all.user.input.digital(数字通道)":[{
            "digitalno":"通道数",
            "digitalname":"采集信号名称",
            "intype":"输入信号输入类型",
            "involt":"输入信号电压"
          }]
        },
      }
    },
    created() {

      if (this.sceneType === 1) {
        this.illustrate = '根据用户输入参数转换成专家系统的输入参数，例如：prior = all.user.prior，all.user代表用户参数对象再.prior，prior代表参数，等号左边prior代表专家系统的输入参数'
        // 参数筛选
        this.sceneTypeString = "FILTER_PARAM";
        this.scriptTypeList = [{"value": "ASSIGNMENT", "name": "赋值"},
          {"value": "CONDITION", "name": "条件"},
          {"value": "CUSTOM", "name": "自定义"}];

      } else if (this.sceneType === 2) {
        this.illustrate = '根据转换后的输入参数，与电路属性进行匹配，筛选出符合条件的电路模板'
        // 电路筛选
        this.sceneTypeString = "CIRCUIT_SELECT";
        this.scriptTypeList = [{"value": "CONDITION", "name": "条件"},
          {"value": "CUSTOM", "name": "自定义"}];

      } else if (this.sceneType === 3) {
        this.illustrate = '根据转换后的输入参数和电路属性，编写规则条件，生成方案'
        // 电路生成
        this.sceneTypeString = "ELEMENT_SELECT";
        this.scriptTypeList = [{"value": "ASSIGNMENT", "name": "赋值"},
          {"value": "RANGE", "name": "范围"},
          {"value": "CONDITION", "name": "条件"},
          {"value": "FORMULA", "name": "公式"},
          {"value": "CUSTOM", "name": "自定义"}];
      }


      // 如果是电路筛选
      if (this.sceneType === 2 || this.sceneType === 3) {
        let query = {
          // circuitTypeId: this.circuitTypeId,
          type: "PROP",
          state: "YES",
        }
        // 获取属性模板
        this.getTemplateList(0, query)

        query = {
          // circuitTypeId: this.circuitTypeId,
          type: "INPUT_PARAM",
          state: "YES",
        }
        // 获取输入参数模板
        this.getTemplateList(1, query)
      }

      if (this.sceneType === 3) {
        /** 查询输入场景参数列表 */
        allListParam({"circuitId": this.sceneId}).then(response => {
          this.paramList = response.data;
        });

        getDicts("expert_component_type").then(res => {
          this.componentTypeList = res.data
        })

      }

      // 获取脚本数据
      this.getScriptList()

    },
    methods: {
      /** 查询脚本列表 */
      getScriptList() {

        let query = {"sceneType": this.sceneTypeString, "sceneId": this.sceneId}

        this.scriptList = []

        listScript(query).then(response => {
          let rows = response.rows;
          for (let i = 0; i < rows.length; i++) {
            let objJson = {}
            // 脚本类型
            if (rows[i].type === 'CONDITION') {
              let conditionList = JSON.parse(rows[i].content);
              objJson = {
                'id': rows[i].id,
                'scriptType': rows[i].type,
                'runScript': "",
                'conditionList': conditionList,
                'priority': rows[i].priority,
                'state': rows[i].state
              }

            } else {
              // 其他类型
              objJson = {
                'id': rows[i].id,
                'scriptType': rows[i].type,
                'runScript': rows[i].content,
                'conditionList': [],
                'priority': rows[i].priority,
                'state': rows[i].state
              }
            }
            this.scriptList.push(objJson)
          }

        });
      },

      /** 查询电路属性参数模板列表 */
      getTemplateList(type, query) {

        allListTemplate(query).then(response => {
          if (type === 0) {
            this.attributeList = response.data;
          } else {
            this.inputParamList = response.data;
          }


        });
      },

      /** 删除脚本 */
      deleteScript(index) {
        if (this.scriptList[index].id !== null && this.scriptList[index].id !== undefined) {
          delScript(this.scriptList[index].id).then(response => {
            if (this.scriptList.length === 1) {
              this.scriptList = []
            } else if (this.scriptList.length > 1) {
              this.scriptList.splice(index, 1)
            }
          });
        } else {
          if (this.scriptList.length === 1) {
            this.scriptList = []
          } else if (this.scriptList.length > 1) {
            this.scriptList.splice(index, 1)
          }
        }

      },
      /** 添加脚本 */
      addScript() {
        let json = {
          'scriptType': this.scriptTypeList[0].value,
          'runScript': "",
          'conditionList': [],
          'priority': this.scriptList.length + 1,
          'state': 'YES'
        }
        this.scriptList.push(json)
      },
      /** 脚本上移 */
      scriptUpward(index) {
        // let priority = this.scriptList[index].priority
        let scriptType = this.scriptList[index].scriptType
        let runScript = this.scriptList[index].runScript
        let conditionList = this.scriptList[index].conditionList
        let state = this.scriptList[index].state

        // this.scriptList[index].priority = this.scriptList[index-1].priority
        this.scriptList[index].scriptType = this.scriptList[index - 1].scriptType
        this.scriptList[index].runScript = this.scriptList[index - 1].runScript
        this.scriptList[index].conditionList = this.scriptList[index - 1].conditionList
        this.scriptList[index].state = this.scriptList[index - 1].state

        // this.scriptList[index-1].priority = priority
        this.scriptList[index - 1].scriptType = scriptType
        this.scriptList[index - 1].runScript = runScript
        this.scriptList[index - 1].conditionList = conditionList
        this.scriptList[index - 1].state = state


      },
      /** 脚本下移 */
      scriptDownward(index) {
        // let priority = this.scriptList[index].priority
        let scriptType = this.scriptList[index].scriptType
        let runScript = this.scriptList[index].runScript
        let conditionList = this.scriptList[index].conditionList
        let state = this.scriptList[index].state

        // this.scriptList[index].priority = this.scriptList[index-1].priority
        this.scriptList[index].scriptType = this.scriptList[index + 1].scriptType
        this.scriptList[index].runScript = this.scriptList[index + 1].runScript
        this.scriptList[index].conditionList = this.scriptList[index + 1].conditionList
        this.scriptList[index].state = this.scriptList[index + 1].state

        // this.scriptList[index-1].priority = priority
        this.scriptList[index + 1].scriptType = scriptType
        this.scriptList[index + 1].runScript = runScript
        this.scriptList[index + 1].conditionList = conditionList
        this.scriptList[index + 1].state = state


      },
      /** 删除条件 */
      deleteCondition(conditionList, index) {
        if (conditionList.length > 1) {
          conditionList.splice(index, 1)
        }
      },
      /** 添加条件 */
      addCondition(conditionList) {
        let json = {'conditionType': 0, 'conditionScript': '', 'priority': conditionList.length + 1, 'execute': ''}
        conditionList.push(json)
      },
      /** 条件上移 */
      conditionUpward(index, conditionList) {
        let conditionType = conditionList[index].conditionType
        let conditionScript = conditionList[index].conditionScript
        let execute = conditionList[index].execute

        conditionList[index].conditionType = conditionList[index - 1].conditionType
        conditionList[index].conditionScript = conditionList[index - 1].conditionScript
        conditionList[index].execute = conditionList[index - 1].execute

        conditionList[index - 1].conditionType = conditionType
        conditionList[index - 1].conditionScript = conditionScript
        conditionList[index - 1].execute = execute
      },
      /** 条件下移 */
      conditionDownward(index, conditionList) {
        let conditionType = conditionList[index].conditionType
        let conditionScript = conditionList[index].conditionScript
        let execute = conditionList[index].execute

        conditionList[index].conditionType = conditionList[index + 1].conditionType
        conditionList[index].conditionScript = conditionList[index + 1].conditionScript
        conditionList[index].execute = conditionList[index + 1].execute

        conditionList[index + 1].conditionType = conditionType
        conditionList[index + 1].conditionScript = conditionScript
        conditionList[index + 1].execute = execute
      },
      /** 获取脚本类型名称 */
      getOneScriptType(val) {
        for (let i = 0; i < this.scriptTypeList.length; i++) {
          if (val == this.scriptTypeList[i].value) {
            return this.scriptTypeList[i].name
          }
        }
      },

      // 保存数据
      save() {
        let data = this.packageScript();

        addAllScript(data, this.sceneId).then(response => {
          this.$modal.msgSuccess("保存成功");
          this.close()
        });
      },
      // 关闭弹窗
      close() {
        this.$emit('close');
      },
      /** 组装脚本 */
      packageScript() {
        let data = []
        for (let i = 0; i < this.scriptList.length; i++) {
          let scriptData = {
            'id': this.scriptList[i].id,
            'sceneId': this.sceneId,
            'sceneType': this.sceneTypeString,
            'content': '',
            'type': 0,
            'state': 1,
            'priority': 0,
          }

          scriptData.type = this.scriptList[i].scriptType
          scriptData.state = this.scriptList[i].state
          if (this.scriptList[i].priority !== undefined && this.scriptList[i].priority != null) {
            scriptData.priority = parseInt(this.scriptList[i].priority)
          }

          if (this.scriptList[i].scriptType === "CONDITION") {
            // 脚本
            scriptData.content = JSON.stringify(this.scriptList[i].conditionList)
          } else {
            // 其他
            scriptData.content = this.scriptList[i].runScript
          }

          data.push(scriptData)

        }
        return data

      },

      // 获取单个元器件类型
      getOneComponentType(val){
        for (let i = 0; i < this.componentTypeList.length; i++){
          if (val == this.componentTypeList[i].dictValue){
            return this.componentTypeList[i].dictLabel
          }
        }
      },
    },

  }
</script>

<style scoped>

</style>
